/*
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.internal.model;

import java.text.MessageFormat;

import net.sourceforge.perforce.core.resources.LocalFile;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;
import net.sourceforge.perforce.ui.model.PerforceModelElement;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * @version $Revision: 28 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class LocalFileElement extends PerforceModelElement implements IWorkbenchAdapter
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Message format for creating the label. */
	private MessageFormat format = new MessageFormat("{0}#{1} <{2}>");
	
	// ---------------------------------------------------------- Public Methods (IWorkbenchAdapter)

	/** @see IWorkbenchAdapter#getChildren(Object) */
	public Object[] getChildren(Object o)
	{
		return new Object[0];
	}

	/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
	public ImageDescriptor getImageDescriptor(Object object)
	{
		if (!(object instanceof LocalFile)) 
		{
			return null;
		}
		
		LocalFile file = (LocalFile) object;
		return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(file.getName());
	}

	/** @see IWorkbenchAdapter#getLabel(Object) */
	public String getLabel(Object object)
	{
		if (!(object instanceof LocalFile)) 
		{
			return null;
		}
		
		LocalFile file = (LocalFile) object;
		ResourceSyncInfo syncInfo = file.getSyncInfo();
		if (syncInfo == null)
		{
			return "!" + file.getName() + "!";
		}
		
		Object[] arguments = new Object[] 
		{ 
			syncInfo.getDepotFileName(), 
			syncInfo.getHaveRev(), 
			syncInfo.getAction() 
		};
		
		return format.format(arguments);
	}

	/** @see IWorkbenchAdapter#getParent(Object) */
	public Object getParent(Object o)
	{
		return null;
	}
}
